package org.springcloud.wisdom.springcloud_03_freshman.admin.service;

import org.springcloud.wisdom.springcloud_03_freshman.admin.dao.AdminTeacherDao;
import org.springcloud.wisdom.springcloud_03_freshman.common.bean.Page;
import org.springcloud.wisdom.springcloud_03_freshman.common.bean.ResponseData;
import org.springcloud.wisdom.springcloud_03_freshman.index.login.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 教师管理
 */
@Service
public class AdminTeacherService {
    @Autowired
    AdminTeacherDao adminTeacherDao;

    /**
     * 处理分页功能：
     * @param pageNum 页码数
     * @param pageSize 每页记录数
     * @param tname 教师姓名
     * @param dept 教师所负责的院系
     * @param tid 职工号
     * @return
     */
    public Page<Teacher> getTeacherPage(int pageNum, int pageSize, String tname, Integer dept, String tid){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Teacher> list=adminTeacherDao.getTeacherByCond(start,pageSize,tname,dept,tid);
        //获取总条目数
        int total=adminTeacherDao.countTeacher(tname,dept,tid);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Teacher> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    /**
     * 添加一位老师
     * @param teacher
     * @return
     */
    public Boolean addT(Teacher teacher){
        if (teacher==null){
            return  false;
        }
        return adminTeacherDao.addTeacher(teacher)>0;
    }

    /**
     * 删除老师
     * @param tid 职工号
     * @return
     */
    public Boolean  deleteT(String tid){
        if (tid==null || tid.trim().length() >8 || tid.trim().length()<8){
            return false;
        }
        return adminTeacherDao.deleteTeacher(tid)>0;
    }

    /**
     * 更新老师的密码 或者所负责的院系
     * @param teacher
     * @return
     */
    public Boolean updateT(Teacher teacher){
        if (teacher==null){
            return false;
        }
        return adminTeacherDao.updateTeacher(teacher)>0;
    }
}
